﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Kitana
{
    class Program
    {
        static void Main(string[] args)
        {
            Score score = new Score();
            Timer timer = new Timer(15);
            //Console.WriteLine(timer);
            while (true)
            {
                if (!timer.updateTimer())
                {
                    Console.WriteLine("Game over!");
                    System.Environment.Exit(1);
                }
                Console.WriteLine("You current score is {0}", score.get());
                score.increment();

                if (score.get() % 100 == 0)
                {
                    score.decrement();
                }
                Thread.Sleep(100);
            }
        }
    }

    class Score
    {
        private const int INCREMENT_VALUE = 10;

        private double currentScore;

        public Score()
        {
            currentScore = 0;
        }

        public double get()
        {
            return currentScore;
        }

        public void increment()
        {
            currentScore += INCREMENT_VALUE;
        }

        public void decrement()
        {
            if (currentScore > 0)
            {
                currentScore /= 2;
            }
        }
    }

    class Timer
    {
        private const int DEFAULT_GAME_TIME = 60; // in seconds 

        private int timeInterval;
        private int startTime;
        private int currentTime;
        private int endTime;
        private int timeLeft;

        public Timer()
        {
            initTimer(DEFAULT_GAME_TIME);
        }

        public Timer(int time) // Timer t = new Timer(40);
        {
            initTimer(time);
        }

        private void initTimer(int time)
        {
            timeInterval = time;

            startTime = getCurrentTime();
            currentTime = startTime;
            endTime = startTime + time;
            Console.WriteLine("Start time is: " + startTime);
            //Console.WriteLine(endTime);
        }

        private int getCurrentTime()
        {
            TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
            return (int)t.TotalSeconds;
        }

        private bool checkTick()
        {
            if (getCurrentTime() > currentTime)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool updateTimer()
        {
            if (checkTick())
            {
                currentTime += 1; // increment with 1 second
                timeLeft = timeInterval - (currentTime - startTime);

                if (timeLeft > 0)
                {
                    Console.WriteLine("interval: {0} curr: {1} start: {2} = {3}", timeInterval, currentTime, startTime,
                    timeLeft);

                    return true;
                }
                else
                {
                    return false; // NOTE: We return false only if time is over!
                }
            }
            else
            {
                return true;
            }
        }
    }
}
